package com.uah;

import java.net.URI;
import java.net.URISyntaxException;

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;

public class Search extends Activity {

	
	
	EditText txtSearch;
	Button btnFind;
	RadioButton radioSpring;
	RadioButton radioSummer;
	RadioButton radioFall;
	RadioButton radioSummer1;
	RadioButton radioSummer2;
	Semester searchSemester = Semester.Spring13;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_search);
	
		txtSearch = (EditText) findViewById(R.id.txtSearch);
		btnFind= (Button) findViewById(R.id.btnFind);
		radioSpring = (RadioButton) findViewById(R.id.radioSpring);
		radioSummer = (RadioButton) findViewById(R.id.radioSummer);
		radioFall = (RadioButton) findViewById(R.id.radioFall);
		radioSummer1 = (RadioButton) findViewById(R.id.radioSummer1);
		radioSummer2 = (RadioButton) findViewById(R.id.radioSummer2);
		
		btnFind.setOnClickListener(btnFindPressedListener);
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.search, menu);
		return true;
	}
	
	
	private OnClickListener btnFindPressedListener = new View.OnClickListener() {
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			String userSearch = txtSearch.getText().toString();

			URI classSchedule = SearchEngine.getInstance().getResults(userSearch, searchSemester);
			//Intent toURL = new Intent(Intent.ACTION_VIEW, Uri.parse(classSchedule.toString()));
			try{
				// Go to app page
				if (classSchedule.toString().charAt(classSchedule.toString().length()-1) == '=')
				{
					Intent toURL = new Intent(v.getContext(), CurrentStudentsPage2.class);
					toURL.putExtra("URL",classSchedule.toString());
					startActivity(toURL);
				}
				else
				{
					Intent toURL = new Intent(v.getContext(), CurrentStudentsPage3.class);
					toURL.putExtra("URL",classSchedule.toString());
					startActivity(toURL);
				}
			}catch(Exception ex){
				Intent toURL = new Intent(Intent.ACTION_VIEW, Uri.parse(classSchedule.toString()));
				startActivity(toURL);
			}

			
		}
	};
	
	public void onRadioButtonClicked(View view) throws Exception{
		
		if (radioSpring.isChecked()){
			searchSemester = Semester.Spring13;
		}
		else if (radioSummer.isChecked()){
			searchSemester = Semester.Summer13R;
			
		}
		else if (radioFall.isChecked()){
			searchSemester = Semester.Fall13;
			
		}
		else if (radioSummer1.isChecked()){
			searchSemester = Semester.Summer13first;
		}
		else if (radioSummer2.isChecked()){
			searchSemester = Semester.Summer13second;
		}
		
		
	}

}
